/*
 * MainController:
 * Contains all the Panels.
 * 
 */

package tetris.controller;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import tetris.view.*;
import tetris.model.*;

public class MainController implements ActionListener{

    public static final int ONE_PLAYER = 1;
    public static final int TWO_PLAYER = 2;
    public static final int NETWORK_GAME = 3;

    private int boardViewXSize = 360;
    private int boardViewYSize = 580;
    private int menuViewXSize = 150;
    private int menuViewYSize = 300;


    private Boolean paused = false;

    private MainWindow mainWindow;

    private BoardController         boardController;
    private BoardModel              boardModel;
    private BoardView               boardView;

    private MenuView                menuView;
    private MenuController          menuController;

    public MainController(){
        mainWindow = new MainWindow("tetris", this, this.menuViewXSize, this.menuViewYSize);
        menuController = new MenuController(this);
        menuView   = new MenuView(menuController);
        addMenu();
        mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void backToMenu(){
        this.paused = true;
        this.boardView.setVisible(false);
        this.mainWindow.remove(boardView);
        this.menuView.showResumeButton(true);
        this.mainWindow.setSize(this.menuViewXSize, this.menuViewYSize);
        addMenu();
    }

    public void backToGame(){
        if(!paused){
            JOptionPane.showMessageDialog(this.mainWindow, "No Game Paused");
            return;
        }
        removeMenu();
        this.mainWindow.setSize(this.boardViewXSize, this.boardViewYSize);
        this.boardView.setVisible(true);
        this.mainWindow.add(boardView);
        this.boardController.resumeGame();
    }
    
    private void addMenu(){
        this.menuView.setVisible(true);
        this.mainWindow.add(menuView);
    }

    private void removeMenu(){
        this.menuView.setVisible(false);
        this.mainWindow.remove(menuView);
    }

    public void newGame(int type){
        System.out.println("New Game, Type: " + type);
        switch(type){
            case 1:
                removeMenu();
                boardModel = new BoardModel(10, 25);
                boardController = new BoardController(boardModel, this);
                boardView = new BoardView(ONE_PLAYER, boardController);
                boardController.connectView(boardView);
                mainWindow.add(boardView);
                this.mainWindow.setSize(this.boardViewXSize, this.boardViewYSize);
                break;
            case 2:
                JOptionPane.showMessageDialog(mainWindow, "Two Player");
                break;
            case 3:
                JOptionPane.showMessageDialog(mainWindow, "Network Game");
                break;
        }
    }
    
    public void actionPerformed(ActionEvent ae){
        
    }
}
